import yaml
import os
from .services.node_registry import register_node_executor

def load_config():
    config_path = os.path.join(os.path.dirname(__file__), '../config.yaml')
    with open(config_path, 'r') as file:
        config = yaml.safe_load(file)
    
    # 动态构造数据库URL
    db_config = config['database']
    db_type = db_config['type']
    if db_type == 'postgresql':
        config['database']['url'] = (
            f"postgresql://{db_config['user']}:{db_config['password']}@"
            f"{db_config['host']}:{db_config['port']}/{db_config['name']}"
        )
    elif db_type == 'mysql':
        config['database']['url'] = (
            f"mysql+pymysql://{db_config['user']}:{db_config['password']}@"
            f"{db_config['host']}:{db_config['port']}/{db_config['name']}"
        )
    else:
        raise ValueError(f"Unsupported database type: {db_type}")
    
    # 加载节点配置并注册
    node_configs = config.get("nodes", {})
    for node_type, details in node_configs.items():
        executor_path = details.get("executor")
        if executor_path:
            register_node_executor(node_type, executor_path)
    
    return config

config = load_config()